package com.zhaowenhao.hello.spring.framework.beans.support;

import com.zhaowenhao.hello.spring.framework.beans.config.WHBeanDefinition;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
 * @Description:
 * @Author: zwh
 * @Date: 2020-08-02
 * @Time: 21:35
 */
public class WHBeanDefinitionReader {
    private static final String SCAN_PACKAGE = "scanPackage";
    //保存扫描到的类名
    private List<String> registyBeanClasses = new ArrayList<String>();

    //读取的配置文件
    Properties config = new Properties();

    public Properties getConfig() {
        return config;
    }

    public WHBeanDefinitionReader(String... locations) {
        //加载配置文件
        InputStream is = null;
        String configLocation = locations[0].replace("classpath:", "/");

        is = this.getClass().getResourceAsStream(configLocation);
        try {
            config.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        doScanner(config.getProperty(SCAN_PACKAGE));
    }

    //扫描所有的配置类
    private void doScanner(String scanPackage) {
        URL url = this.getClass().getResource("/" + scanPackage.replaceAll("\\.", "/"));
        File file = new File(url.getFile());
        for (File f : file.listFiles()) {
            if (f.isDirectory()) {
                doScanner(scanPackage + "." + f.getName());
            } else {
                if (!f.getName().endsWith(".class")) {
                    continue;
                }
                String clazzName = scanPackage + "." + f.getName().replace(".class", "");
                registyBeanClasses.add(clazzName);
            }
        }
    }

    //为所有bean创建其BeanDefinition
    public List<WHBeanDefinition> loadBeanDefinitions() {
        List<WHBeanDefinition> result = new ArrayList<WHBeanDefinition>();

        for (String className : registyBeanClasses) {
            try {
                Class<?> clazz = Class.forName(className);
                //跳过接口，因为接口是不能实例化的
                if (clazz.isInterface()) {
                    continue;
                }

                result.add(doCreatBeanDefinition(toLowerFirstCase(clazz.getSimpleName()), clazz.getName()));

                Class<?>[] interfaces = clazz.getInterfaces();
                for (Class i : interfaces) {
                    result.add(doCreatBeanDefinition(i.getName(), clazz.getName()));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    //创建一个beanDefinition
    private WHBeanDefinition doCreatBeanDefinition(String factoryBeanName, String beanClassName) {
        return new WHBeanDefinition(factoryBeanName, beanClassName);
    }


    private String toLowerFirstCase(String simpleName) {
        char[] chars = simpleName.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }


}
